package com.stu.controller;

import com.bejson.pojo.vm.ResVirtualMachine;
import com.google.common.collect.Lists;
import com.stu.service.impl.HostSystemManager;
import com.vmware.vim25.VirtualMachineUsageOnDatastore;
import com.vmware.vim25.mo.Datastore;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.VirtualMachine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.List;

/**
 * @projectName: learn_used_code
 * @packageName: com.stu.controller
 * @className: com.stu.controller-> TestVmController
 * @createDate: 2022-03-22 18:17
 * @author: ZhangSheng
 * @version: 1.0
 * @description: 测试虚机
 */
@RestController
@RequestMapping(value = "/vm")
public class TestVmController {

    @Autowired
    private HostSystemManager hostSystemManager;

    @RequestMapping(value = "/getVmList",method = RequestMethod.GET)
    public List<ResVirtualMachine> testVmList() throws MalformedURLException, RemoteException {
        List<ResVirtualMachine> vmList = Lists.newArrayList();
        ServiceInstance connection = hostSystemManager.connection("192.168.157.2", 443, "frank@vsphere.local", "frank@123");
        List<VirtualMachine> virtualMachineList = hostSystemManager.getVirtualMachineList(connection);
        for (VirtualMachine virtualMachine : virtualMachineList) {
            ResVirtualMachine resVirtualMachine = new ResVirtualMachine();
            resVirtualMachine.setInstanceId(virtualMachine.getConfig().getInstanceUuid());
            resVirtualMachine.setMorId(virtualMachine.getMOR().getVal());
            resVirtualMachine.setCpuSize(virtualMachine.getSummary().getConfig().getNumCpu());
            resVirtualMachine.setMenmerySize(virtualMachine.getSummary().getConfig().getMemorySizeMB());
            resVirtualMachine.setPowerStatus(virtualMachine.getRuntime().getPowerState().name());
            resVirtualMachine.setName(virtualMachine.getName());
            resVirtualMachine.setIpAdress(virtualMachine.getSummary().getGuest().ipAddress);
            long capacityTotal = 0L;
            for (Datastore datastore : virtualMachine.getDatastores()) {
                long capacity = datastore.getSummary().capacity;
                capacityTotal += capacity;
            }
            resVirtualMachine.setDiskSize(capacityTotal);
            vmList.add(resVirtualMachine);
        }
        return vmList;
    }

    @RequestMapping(value = "/singleVm/{morId}",method = RequestMethod.GET)
    public ResVirtualMachine testSigleVmInfo(@PathVariable String morId) throws MalformedURLException, RemoteException {
        ServiceInstance instance = hostSystemManager.connection("192.168.157.2", 443, "frank@vsphere.local", "frank@123");
        VirtualMachine virtualMachine = hostSystemManager.getSingleVm(instance, morId);
        if (ObjectUtils.isEmpty(virtualMachine)){
            return null;
        }
        ResVirtualMachine resVirtualMachine = new ResVirtualMachine();
        VirtualMachineUsageOnDatastore[] perDatastoreUsage = virtualMachine.getStorage().getPerDatastoreUsage();
        for (int i = 0; i < perDatastoreUsage.length; i++) {
            String data_store = perDatastoreUsage[i].getDatastore().getVal();
            long committed = perDatastoreUsage[i].getCommitted();
            System.out.println("data_store:" + data_store + " , committed:" + committed);
        }
        resVirtualMachine.setInstanceId(virtualMachine.getConfig().getInstanceUuid());
        resVirtualMachine.setMorId(virtualMachine.getMOR().getVal());
        resVirtualMachine.setCpuSize(virtualMachine.getSummary().getConfig().getNumCpu());
        resVirtualMachine.setMenmerySize(virtualMachine.getSummary().getConfig().getMemorySizeMB());
        resVirtualMachine.setPowerStatus(virtualMachine.getRuntime().getPowerState().name());
        resVirtualMachine.setName(virtualMachine.getName());
        resVirtualMachine.setIpAdress(virtualMachine.getSummary().getGuest().ipAddress);
        long capacityTotal = 0L;
        for (Datastore datastore : virtualMachine.getDatastores()) {
            long capacity = datastore.getSummary().capacity;
            capacityTotal += capacity;
        }
        resVirtualMachine.setDiskSize(capacityTotal);
        return resVirtualMachine;
    }


}
